<?php   if(!defined('DEDEINC')) exit('Request Error!');
/**
 * 分类信息地区与类型快捷链接
 *
 * @version        $Id: infolink.lib.php 1 9:29 2010年7月6日Z tianya $
 * @package        DedeCMS.Taglib
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
 
/*>>dede>>
<name>分类信息地区与类型快捷链接</name>
<type>全局标记</type>
<for>V55,V56,V57</for>
<description>调用分类信息地区与类型快捷链接</description>
<demo>
{dede:infolink /}
</demo>
<attributes>
</attributes> 
>>dede>>*/
 
require_once(DEDEINC.'/enums.func.php');
require_once(DEDEDATA.'/enums/nativeplace.php');
require_once(DEDEDATA.'/enums/infotype.php');

require_once(DEDEDATA.'/enums/s4jg.php'); // s4价格
require_once(DEDEDATA.'/enums/s4pp.php'); // s4品牌
require_once(DEDEDATA.'/enums/level.php'); // s4级别
require_once(DEDEDATA.'/enums/area.php'); // s4地区

function lib_infolink(&$ctag,&$refObj)
{
    global $dsql,$nativeplace,$infotype,$hasSetEnumJs,$cfg_cmspath,$cfg_mainsite,$s4jg,$s4pp,$level,$area;
	//echo $nativeplace.'=='.$s4pp;
    global $em_nativeplaces,$em_infotypes,$em_s4jgs,$em_s4pps,$em_levels,$em_areas; // qin 增加全局变量 $em_s4jgs,$em_s4pps,$em_levels,$em_areas
    
    //属性处理
    //$attlist="row|12,titlelen|24";
    //FillAttsDefault($ctag->CAttribute->Items,$attlist);
    //extract($ctag->CAttribute->Items, EXTR_SKIP);
    
    $cmspath = ( (empty($cfg_cmspath) || !preg_match("#\/$#", $cfg_cmspath)) ? $cfg_cmspath.'/' : $cfg_cmspath );
    $baseurl = preg_replace("#\/$#", '', $cfg_mainsite).$cmspath;
    
    $smalltypes = '';
    if( !empty($refObj->TypeLink->TypeInfos['smalltypes']) ) {
        $smalltypes = explode(',', $refObj->TypeLink->TypeInfos['smalltypes']);
    }
    
    if(empty($refObj->Fields['typeid'])) {
        $row = $dsql->GetOne("SELECT id FROM `#@__arctype` WHERE channeltype='-8' And reid = '0' ");
        $typeid = (is_array($row) ? $row['id'] : 0);
    }
    else {
        $typeid = $refObj->Fields['typeid'];
    }
    
    $innerText = trim($ctag->GetInnerText());
    if(empty($innerText)) $innerText = GetSysTemplets("info_link.htm");
    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field','[',']');
    $ctp->LoadSource($innerText);

    $revalue = $seli = '';
    $channelid = ( empty($refObj->TypeLink->TypeInfos['channeltype']) ? -8 : $refObj->TypeLink->TypeInfos['channeltype'] );
    
	// 增加字段 qin
    $fields = array('nativeplace'=>'','infotype'=>'','s4jg'=>'','s4pp'=>'','level'=>'','area'=>'','typeid'=>$typeid,
                    'channelid'=>$channelid,'linkallplace'=>'','linkalltype'=>'','linkalljg'=>'','linkallpp'=>'','linkalllevel'=>'','linkallarea'=>'');
    
    $fields['nativeplace'] = $fields['infotype']= $fields['s4jg']= $fields['s4pp'] = '';
    $fields['linkallplace'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&infotype={$infotype}'>不限</a>";
    $fields['linkalltype'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&nativeplace={$nativeplace}'>不限</a>";
    
	// qin 价格和品牌
	 $fields['linkalljg'] = "<a  onclick=\"setJgHref(this,'&s4jg')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$s4pp}&level={$level}&area={$area}'>不限</a>\r\n";
     $fields['linkallpp'] = "<a  href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$s4jg}&level={$level}&area={$area}'>不限</a>\r\n";
     $fields['linkalllevel'] = "<a  href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$s4pp}&s4jg={$s4jg}&area={$area}'>不限</a>\r\n";
     $fields['linkallarea'] = "<a  onclick=\"setAreaHref(this,'&area')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$s4jg}&s4pp={$s4pp}&level={$level}'>不限</a>\r\n";
    
	
	// s4价格链接 
    if(empty($s4jg))
    {
	    $fields['s4jg'] ="<a onclick=\"setJgHref(this,'&s4jg')\" class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$s4pp}&level={$level}&area={$area}'>不限</a>\r\n".$fields['s4jg'];
        foreach($em_s4jgs as $eid=>$em)
        {
            if($eid % 500 != 0) continue;
            $fields['s4jg'] .= " <a onclick=\"setJgHref(this,'&s4jg')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$eid}&s4pp={$s4pp}&infotype={$infotype}&level={$level}&area={$area}'>{$em}</a>\r\n";
        }
    }
    else
    {
        $sontype = ( ($s4jg % 500 != 0) ? $s4jg : 0 );
        $toptype = ( ($s4jg % 500 == 0) ? $s4jg : ( $s4jg-($s4jg%500) ) );
		//2011-6-21 修改地区列表的一个小空格 论坛http://bbs.dedecms.com/371492.html(by：织梦的鱼)
      //  $fields['s4jg'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$toptype}&infotype={$infotype}'> <b>{$em_s4jgs[$toptype]}</b></a> ";
        foreach($em_s4jgs as $eid=>$em)
        {
           // if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $s4jg) {
                //$fields['s4jg'] .= " <b>{$em}</b>\r\n";
				$fields['s4jg'] .= " <a  onclick=\"setJgHref(this,'&s4jg')\" class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$eid}&s4pp={$s4pp}&infotype={$infotype}&level={$level}&area={$area}'><b>{$em}</b></a>\r\n";

            }else {
                $fields['s4jg'] .= " <a onclick=\"setJgHref(this,'&s4jg')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$eid}&s4pp={$s4pp}&infotype={$infotype}&level={$level}&area={$area}'>{$em}</a>\r\n";
            }
        }
		// echo $fields['linkalljg'];
		$fields['s4jg'] = $fields['linkalljg'].$fields['s4jg'];
    }
	
	// s4品牌链接
    if(empty($s4pp))
    {
	    $fields['s4pp'] = "<a class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$s4jg}&level={$level}&area={$area}'>不限</a>\r\n".$fields['s4pp'];
        foreach($em_s4pps as $eid=>$em)
        {
            if($eid % 500 != 0) continue;
            $fields['s4pp'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&area={$area}'>{$em}</a>\r\n";
        }
    }
    else
    {
        $sontype = ( ($s4pp % 500 != 0) ? $s4pp : 0 );
        $toptype = ( ($s4pp % 500 == 0) ? $s4pp : ( $s4pp-($s4pp%500) ) );
		//2011-6-21 修改地区列表的一个小空格 论坛http://bbs.dedecms.com/371492.html(by：织梦的鱼)
       // $fields['s4pp'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$toptype}&infotype={$infotype}'> <b>{$em_s4pps[$toptype]}</b></a> ";
        foreach($em_s4pps as $eid=>$em)
        {
            //if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $s4pp) {
               // $fields['s4pp'] .= " <b>{$em}</b>\r\n";
				$fields['s4pp'] .= " <a class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&area={$area}'><b>{$em}</b></a>\r\n";
            }
            else {
                $fields['s4pp'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4pp={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&area={$area}'>{$em}</a>\r\n";
            }
			
        }
	    $fields['s4pp'] = $fields['linkallpp'].$fields['s4pp'];
    }
	
	// s4地区
    if(empty($area))
    {
	    $fields['area'] = "<a onclick=\"setAreaHref(this,'&area')\" class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$s4jg}&level={$level}&s4pp={$s4pp}'>不限</a>\r\n".$fields['area'];
        foreach($em_areas as $eid=>$em)
        {
            if($eid % 500 != 0) continue;
            $fields['area'] .= " <a onclick=\"setAreaHref(this,'&area')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&area={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&s4pp={$s4pp}'>{$em}</a>\r\n";
        }
    }
    else
    {
        $sontype = ( ($area % 500 != 0) ? $area : 0 );
        $toptype = ( ($area % 500 == 0) ? $area : ( $area-($area%500) ) );
		//2011-6-21 修改地区列表的一个小空格 论坛http://bbs.dedecms.com/371492.html(by：织梦的鱼)
       // $fields['area'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&area={$toptype}&infotype={$infotype}'> <b>{$em_areas[$toptype]}</b></a> ";
        foreach($em_areas as $eid=>$em)
        {
            //if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $area) {
               // $fields['area'] .= " <b>{$em}</b>\r\n";
				$fields['area'] .= " <a onclick=\"setAreaHref(this,'&area')\" class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&area={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&s4pp={$s4pp}'><b>{$em}</b></a>\r\n";
            }
            else {
                $fields['area'] .= " <a onclick=\"setAreaHref(this,'&area')\" href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&area={$eid}&s4jg={$s4jg}&infotype={$infotype}&level={$level}&s4pp={$s4pp}'>{$em}</a>\r\n";
            }
			
        }
	    $fields['area'] = $fields['linkallarea'].$fields['area'];
    }
	
	
	// s4级别
    if(empty($level))
    {
	    $fields['level'] = "<a class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&s4jg={$s4jg}&area={$area}&s4pp={$s4pp}'>不限</a>\r\n".$fields['level'];
        foreach($em_levels as $eid=>$em)
        {
            if($eid % 500 != 0) continue;
            $fields['level'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&level={$eid}&s4jg={$s4jg}&infotype={$infotype}&area={$area}&s4pp={$s4pp}'>{$em}</a>\r\n";
        }
    }
    else
    {
        $sontype = ( ($level % 500 != 0) ? $level : 0 );
        $toptype = ( ($level % 500 == 0) ? $level : ( $level-($level%500) ) );
		//2011-6-21 修改地区列表的一个小空格 论坛http://bbs.dedecms.com/371492.html(by：织梦的鱼)
       // $fields['level'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&level={$toptype}&infotype={$infotype}'> <b>{$em_levels[$toptype]}</b></a> ";
        foreach($em_levels as $eid=>$em)
        {
            //if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $level) {
               // $fields['level'] .= " <b>{$em}</b>\r\n";
				$fields['level'] .= " <a class='selected' href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&level={$eid}&s4jg={$s4jg}&infotype={$infotype}&area={$area}&s4pp={$s4pp}'><b>{$em}</b></a>\r\n";
            }
            else {
                $fields['level'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&level={$eid}&s4jg={$s4jg}&infotype={$infotype}&area={$area}&s4pp={$s4pp}'>{$em}</a>\r\n";
            }
			
        }
	    $fields['level'] = $fields['linkalllevel'].$fields['level'];
    }
	
    //地区链接
    if(empty($nativeplace))
    {
        foreach($em_nativeplaces as $eid=>$em)
        {
            if($eid % 500 != 0) continue;
            $fields['nativeplace'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&nativeplace={$eid}&infotype={$infotype}'>{$em}</a>\r\n";
        }
    }
    else
    {
        $sontype = ( ($nativeplace % 500 != 0) ? $nativeplace : 0 );
        $toptype = ( ($nativeplace % 500 == 0) ? $nativeplace : ( $nativeplace-($nativeplace%500) ) );
		//2011-6-21 修改地区列表的一个小空格 论坛http://bbs.dedecms.com/371492.html(by：织梦的鱼)
        $fields['nativeplace'] = "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&nativeplace={$toptype}&infotype={$infotype}'> <b>{$em_nativeplaces[$toptype]}</b></a> &gt;&gt; ";
        foreach($em_nativeplaces as $eid=>$em)
        {
            if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $nativeplace) {
                $fields['nativeplace'] .= " <b>{$em}</b>\r\n";
            }
            else {
                $fields['nativeplace'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&nativeplace={$eid}&infotype={$infotype}'>{$em}</a>\r\n";
          }
      }
    }
    //小分类链接
    if(empty($infotype) || is_array($smalltypes))
    {
        
        foreach($em_infotypes as $eid=>$em)
        {
            if(!is_array($smalltypes) && $eid % 500 != 0) continue;
            if(is_array($smalltypes) && !in_array($eid, $smalltypes)) continue;
            if($eid == $infotype) 
            {
                $fields['infotype'] .= " <b>{$em}</b>\r\n";
            }
            else {
                $fields['infotype'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&infotype={$eid}&nativeplace={$nativeplace}'>{$em}</a>\r\n";
            }
        }
    }
    else
    {
        $sontype = ( ($infotype % 500 != 0) ? $infotype : 0 );
        $toptype = ( ($infotype % 500 == 0) ? $infotype : ( $infotype-($infotype%500) ) );
        $fields['infotype'] .= "<a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&infotype={$toptype}&nativeplace={$nativeplace}'><b>{$em_infotypes[$toptype]}</b></a> &gt;&gt; ";
        foreach($em_infotypes as $eid=>$em)
        {
            if($eid < $toptype+1 || $eid > $toptype+499) continue;
            if($eid == $infotype) {
                $fields['infotype'] .= " <b>{$em}</b>\r\n";
            }
            else {
                $fields['infotype'] .= " <a href='{$baseurl}plus/list.php?channelid={$channelid}&tid={$typeid}&infotype={$eid}&nativeplace={$nativeplace}'>{$em}</a>\r\n";
          }
      }
    }
    
    
    if(is_array($ctp->CTags))
    {
        foreach($ctp->CTags as $tagid=>$ctag)
        {
            if(isset($fields[$ctag->GetName()])) {
                $ctp->Assign($tagid,$fields[$ctag->GetName()]);
            }
        }
        $revalue .= $ctp->GetResult();
    }
    
    return $revalue;
}